let storage = bui.storage();
let language;
let translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            translate = global.translate
            language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            if (language === "chinese_simplified") {
                router.$("#J_Form").css("font-size", "")
                router.$(".bui-input").css("font-size", "")
                router.$("#nameFb").css("font-size", "")
            } else {
                router.$("#J_Form").css("font-size", ".22rem")
                router.$(".bui-input").css("font-size", ".22rem")
                router.$("#nameFb").css("font-size", ".3rem")
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })


            let uiLoading = bui.loading({
                appendTo:"#endEditLoading",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"加载中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });
            let uiLoadingBtn = bui.loading({
                appendTo:"#endEditLoading",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"加载中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });

            let token = storage.get("token",0)
            let id;
            let wxEndStatus = true, wxVipStatus = true, contentStatus = true

            router.$("#footerFh").click(function () {
                storage.set("tzPage", '0')
                router.replace({ url: "pages/mainIndex/mainIndex.html"});
            })

            // let wxEnd = bui.input({
            //     id: "#wxEndInput",
            //     showIcon: false,
            //     onInput: function(e) {
            //         if (e.target.value.length > 255) {
            //             wxEndStatus = false
            //             router.$("#wxEndTs").html("客服微信最大255字符");
            //             return false;
            //         } else {
            //             wxEndStatus = true
            //             router.$("#wxEndTs").html("");
            //         }
            //         return true;
            //     }
            // })
            // let wxVip = bui.input({
            //     id: "#wxVipInput",
            //     showIcon: false,
            //     onInput: function(e) {
            //         if (e.target.value.length > 255) {
            //             wxVipStatus = false
            //             router.$("#wxVipTs").html("VIP客服微信最大255字符")
            //             return false
            //         } else {
            //             wxVipStatus = true
            //             router.$("#wxVipTs").html("")
            //         }
            //         return true;
            //     }
            // })
            // 内容的长度限制
            let contentEnd = bui.input({
                id: "#contentEnd",
                target: "textarea",
                showIcon: false,
                showLength: true,
                onInput: function(e) {
                    if (e.target.value.length > 500) {
                        contentStatus = false
                        router.$("#contentTs").html("文末内容最大500字符")
                        return false
                    } else {
                        contentStatus = true
                        router.$("#contentTs").html("")
                    }
                    return true;
                }
            })
            contentEnd.value('往下拉可看其他同类信息。点击图片后可放大。[别忘了说在海外同城看到的哦]现在关注我们可免费帮您发布便民信息及进入官方微信群\n' +
                '海外同城是信息发布和传播平台，请您自行评估交易风险若有任何交易，请自行核实对方身份及信息的真伪。若引起任何纠纷及法律责任，均与本平台无关，本平台不承担任何责任。\n' +
                '本微信号已获海外同城官方授权，仿冒必究。如有任何过期、虚假或错误的信息，请及时通知我们删除或修改纠正。本平台享有最终解释权。\n' +
                '有需要广告推广，请联系海外同城')

            if (token) {
                uiLoading.show()
                bui.ajax({
                    url: global.api.verifyToken,
                    method: "POST",
                    headers: {Token: token}
                }).then(function(res){
                    if (res.code != '200') {
                        uiLoading.stop()
                        bui.confirm({
                            "title": "",
                            "height": 460,
                            "content": '<div class="bui-box-center"><div><h3>登录已过期</h3></div></div>',
                            "buttons": [{ name: "确定", className: "primary-reverse" }],
                            callback:function(){
                                this.close();
                                storage.set("dlTz", '14')
                                router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                            }
                        });
                    }
                })
                bui.ajax({
                    url: global.api.getFooterInfo,
                    data: {},
                    method: "GET",
                    headers: {Token: token}
                }).then(function(res){
                    if (res.hasOwnProperty("data")) {
                        let data = res.data
                        if (data.content) {
                            contentEnd.value(data.content)
                        }
                        // router.$("#wxEnd").val(data.wx)
                        // router.$("#wxVip").val(data.wxVip)
                        id = data.id
                    }
                    uiLoading.stop()
                })
            } else {
                bui.confirm({
                    "title": "",
                    "height": 460,
                    "content": '<div class="bui-box-center"><div><h3>当前未登录，请登录</h3></div></div>',
                    "buttons": [{ name: "登录", className: "primary-reverse" }],
                    callback:function(){
                        this.close();
                        storage.set("dlTz", '14')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                });
            }

            router.$("#wxSubmit").click(function () {
                // if (wxEndStatus && wxVipStatus) {
                if (contentStatus) {
                    uiLoadingBtn.show()
                    // bui.btn("#wxSubmit").submit(function(loading) {
                        if (id) {
                            bui.ajax({
                                url: global.api.editFooter,
                                // data: {content: contentEnd.value(),wx: wxEnd.value(),wxVip: wxVip.value(), id: id},
                                data: {content: contentEnd.value(), id: id},
                                method: "POST",
                                headers: {Token: token}
                            }).then(function(res){
                                if (res.code == '200') {
                                    // 修改数据
                                    bui.confirm("提交成功！", function(e) {
                                        //this 是指点击的按钮
                                        let text = router.$(e.target).text();

                                        if (text == "确定") {
                                            storage.set("tzPage", '0')
                                            router.replace({ url: "pages/mainIndex/mainIndex.html"});
                                        }
                                        this.close()
                                    });
                                } else {
                                    bui.alert("提交失败！")
                                }
                                uiLoadingBtn.stop()
                                // loading.stop()
                            })
                        } else {
                            bui.ajax({
                                url: global.api.insertFooter,
                                // data: {content: contentEnd.value(),wx: wxEnd.value(),wxVip: wxVip.value()},
                                data: {content: contentEnd.value()},
                                method: "POST",
                                headers: {Token: token}
                            }).then(function(res){
                                if (res.code == '200') {
                                    // 修改数据
                                    bui.confirm("提交成功！", function(e) {
                                        //this 是指点击的按钮
                                        let text = router.$(e.target).text();

                                        if (text == "确定") {
                                            storage.set("tzPage", '0')
                                            router.replace({ url: "pages/mainIndex/mainIndex.html"});
                                        }
                                        this.close()
                                    });
                                } else {
                                    bui.alert("提交失败！")
                                }
                                uiLoadingBtn.stop()
                                // loading.stop()
                            })
                        }
                    // })
                } else {
                    router.$("#contentTs").html("文末内容最大500字符")
                    bui.alert("数据不符合格式，请检查！")
                }
            })
        }
    };

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})
